#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/opencv.hpp>

#include <iostream>
#include <string>

using namespace std;
using namespace cv;

void blur(Mat &Sobel, Mat &frame)
{
    //图像的平均灰度
    double meanValue = 0.0;
    meanValue = mean(Sobel)[0];
    //imshow("CannyImg", );
    //double to string
    stringstream meanValueStream;
    string meanValueString;
    meanValueStream << meanValue;
    meanValueStream >> meanValueString;
    meanValueString = "Fuzzy anomaly" + meanValueString;
    cout << meanValueString << endl;
    if (meanValue < 1.0)
    {
        printf("模糊\n");
        putText(frame, meanValueString, Point(20, 50), CV_FONT_HERSHEY_COMPLEX, 0.8, Scalar(255, 255, 25), 2);
    }
}

int main() //blur
{
    //打开视频文件：其实就是建立一个VideoCapture结构
    VideoCapture capture("/Users/lukeskywalker/Desktop/blur.mov");
    //检测是否正常打开:成功打开时，isOpened返回ture
    if (!capture.isOpened())
        cout << "fail to open!" << endl;
    //定义一个用来控制读取视频循环结束的变量
    bool stop = false;
    //承载每一帧的图像
    Mat frame;
    Mat gray;
    Mat Sobel;
    Mat tempframe;
    int framenum = 0;

    while (!stop)
    {
        //读取下一帧
        if (!capture.isOpened())
        {
            cout << "读取视频失败" << endl;
            return -1;
        }

        capture >> frame;
        tempframe = frame;
        framenum++;

        if (framenum == 1)
        {
            cvtColor(tempframe, gray, CV_BGR2GRAY);
            Laplacian(gray, Sobel, CV_16U);
        }
        if (framenum >= 2)
        {
            cvtColor(tempframe, gray, CV_BGR2GRAY); //转化为单通道灰度图，此时currentFrame已经存了tempFrame的内容
            Laplacian(gray, Sobel, CV_16U);

            //显示图像
            //imshow("camera", tempframe);
            //imshow("moving area", gray);
        }

        if (!(framenum % 1))
        {
            printf("f:%d\n", framenum);
            //***suanfa;
        }

        blur(Sobel, frame);
        imshow("Articulation", frame);

        int c = waitKey(10);
        //按下ESC或者到达指定的结束帧后退出读取视频
        if ((char)c == 27)
        {
            stop = true;
        }
    }
    //关闭视频文件
    capture.release();
    waitKey(0);
    return 0;
}
